package com.ele.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ele.dao.FoodCategoryDao;
import com.ele.dao.FoodDao;
import com.ele.dao.impl.FoodCategoryDaoImpl;
import com.ele.dao.impl.FoodDaoImpl;
import com.ele.pojo.Food;
import com.ele.pojo.FoodCategory;
import com.ele.pojo.PageResult;
import com.ele.pojo.vo.FoodVO;
import com.ele.service.FoodService;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodServiceImpl implements FoodService {
    private FoodDao foodDao = new FoodDaoImpl();
    private FoodCategoryDao foodCategoryDao = new FoodCategoryDaoImpl();
    @Override
    public PageResult<List<FoodVO>> findFood(String currentPage, String pageSize, String categoryId, String shopId , String foodName) {
        try {
            // 查询页面需要的数据
            List<Food> foods = this.foodDao.findFoodList(currentPage , pageSize , categoryId , shopId ,foodName);
            List<FoodVO> foodVOS = new ArrayList<>();
            for (Food food : foods) {
                FoodVO foodVO = new FoodVO();
                FoodCategory foodCategory = this.foodCategoryDao.findFoodCategoryById(food.getCategoryId());
                BeanUtil.copyProperties(food,foodVO);
                foodVO.setCategoryName(foodCategory.getCategoryName());
                foodVOS.add(foodVO);
            }
            Long total = this.foodDao.foodCount(shopId);
            PageResult<List<FoodVO>> pageResult = new PageResult<>();
            pageResult.setDatas(foodVOS);
            pageResult.setTotal(total);
            return pageResult;
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer addFood(Food food) {
        try {
            return this.foodDao.addFood(food);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer updateFood(Food food) {
        try {
            return this.foodDao.updateFood(food);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer deleteFood(String foodId) {
        try {
            return this.foodDao.deleteFood(foodId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }
}
